package com.caifu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.caifu.mapper.CDOrgMapper;
import com.caifu.mapper.CDeviceMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.caifu.pojo.CDOrg;
import com.caifu.pojo.CDevice;
import com.caifu.service.CDeviceService;
import com.caifu.util.Constance;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 设备信息表 服务实现类
 * </p>
 *
 * @author tww
 * @since 2020-12-10
 */
@Service
public class CDeviceServiceImpl extends ServiceImpl<CDeviceMapper, CDevice> implements CDeviceService {

    @Resource
    private CDeviceMapper cDeviceMapper;
    @Resource
    private CDOrgMapper orgMapper;


    @Override
    public List<Map<String, Object>> listDevice(Map map) throws Exception {
        try {
            return cDeviceMapper.listDevice(map);
        } catch (Exception e){
            e.printStackTrace();
            throw new Exception();
        }
    }

    @Override
    public List<Map<String, Object>> listOrgDevice(Map map) throws Exception {
        try {
            return cDeviceMapper.listOrgDevice(map);
        } catch (Exception e){
            e.printStackTrace();
            throw new Exception();
        }
    }

    @Override
    public List<Map<String, Object>> listDeptDevice(Map map) throws Exception {
        try {
            return cDeviceMapper.listDeptDevice(map);
        } catch (Exception e){
            e.printStackTrace();
            throw new Exception();
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public Map addDeviceInfo(CDevice device) throws Exception {
        Map map = new HashMap();
        try {
            //校验设备序列号
            int num = 0;
            String ids = device.getIds();
            device.setCreateTime(Constance.currentDateTime());
            device.setStatus("0");
            num = cDeviceMapper.insert(device);
            if(num ==1){
                //添加设备关联机构
                if(ids != null && !"".equals(ids)){
                    List<CDOrg> list = new ArrayList<>();
                    String[] str = ids.split(",");
                    for (String id : str) {
                        CDOrg org = new CDOrg();
                        org.setOrgId(Integer.valueOf(id));
                        org.setDeviceId(device.getId());
                        org.setCreateTime(Constance.currentDateTime());
                        org.setStatus("1");
                        list.add(org);
                    }
                    list.forEach(model -> orgMapper.insert(model));
                }
                map.put("code", 200);
                map.put("msg", "添加成功！");
            } else {
                map.put("code", 302);
                map.put("msg", "添加失败！");
            }
        } catch (Exception e){
            e.printStackTrace();
            map.put("code", 998);
            map.put("msg", "添加异常！");
            throw new Exception();
        }
        return map;
    }

    @Override
    public CDevice getDeviceInfo(int id) throws Exception {
        try {
            return cDeviceMapper.selectById(id);
        } catch (Exception e){
            e.printStackTrace();
            throw new Exception();
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public Map editDeviceInfo(CDevice device) throws Exception {
        Map map = new HashMap();
        try {
            CDevice old = cDeviceMapper.selectById(device.getId());
            //校验设备序列号
            int num = cDeviceMapper.countDeviceById(device.getDeviceUid(), device.getId()+"");
            if(num >0){
                map.put("code", 301);
                map.put("msg", "设备已存在，请勿重复添加！");
            } else {
                String ids = device.getIds();
                device.setCreateTime(old.getCreateTime());
                device.setStatus(old.getStatus());
                BeanUtils.copyProperties(device, old);
                num = cDeviceMapper.updateById(old);
                if(num ==1){
                    //删除原有关联机构
                    QueryWrapper<CDOrg> orgQueryWrapper = new QueryWrapper<>();
                    orgQueryWrapper.eq("DEVICE_ID", device.getId());
                    orgQueryWrapper.eq("STATUS", "1");
                    List<CDOrg> list = orgMapper.selectList(orgQueryWrapper);
                    for (CDOrg org : list) {
                        org.setStatus("9");
                    }
                    list.forEach(model -> orgMapper.updateById(model));

                    //添加设备关联机构
                    if(ids != null && !"".equals(ids)){
                        list = new ArrayList<>();
                        String[] str = ids.split(",");
                        for (String id : str) {
                            CDOrg org = new CDOrg();
                            org.setOrgId(Integer.valueOf(id));
                            org.setDeviceId(device.getId());
                            org.setCreateTime(Constance.currentDateTime());
                            org.setStatus("1");
                            list.add(org);
                        }
                        list.forEach(model -> orgMapper.insert(model));
                    }
                    map.put("code", 200);
                    map.put("msg", "保存成功！");
                } else {
                    map.put("code", 302);
                    map.put("msg", "保存失败！");
                }
            }
        } catch (Exception e){
            e.printStackTrace();
            map.put("code", 998);
            map.put("msg", "保存异常！");
            throw new Exception();
        }
        return map;
    }

    @Override
    public int countDevice(Map map) throws Exception {
        try {
            return cDeviceMapper.countDevice(map);
        } catch (Exception e){
            e.printStackTrace();
            throw new Exception();
        }
    }

    @Override
    public int countOrgDevice(Map map) throws Exception {
        try {
            return cDeviceMapper.countDeviceByOrg(map);
        } catch (Exception e){
            e.printStackTrace();
            throw new Exception();
        }
    }

    @Override
    public int countDeptDevice(Map map) throws Exception {
        try {
            return cDeviceMapper.countDeviceByDept(map);
        } catch (Exception e){
            e.printStackTrace();
            throw new Exception();
        }
    }

    @Override
    public List<Map<String, Object>> getUserList(Map map) throws Exception {
        try {
            return cDeviceMapper.getUserList(map);
        } catch (Exception e){
            e.printStackTrace();
            throw new Exception();
        }
    }

    @Override
    public int countUserList(Map map) throws Exception {
        try {
            return cDeviceMapper.countUserList(map);
        } catch (Exception e){
            e.printStackTrace();
            throw new Exception();
        }
    }

    @Override
    public Map delDeviceInfo(int id) throws Exception {
        Map map = new HashMap();
        try {
            CDevice device = cDeviceMapper.selectById(id);
            device.setStatus("9");
            int num = cDeviceMapper.updateById(device);
            if(num == 1){
                map.put("code", 0);
                map.put("msg", "操作成功！");
            } else {
                map.put("code", 9);
                map.put("msg", "操作失败！");
            }
        } catch (Exception e){
            e.printStackTrace();
            map.put("code", 998);
            map.put("msg", "操作异常！");
            throw new Exception();
        }
        return map;
    }

}
